<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        title="提示"
        :visible.sync="showDialog"
        width="50%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <addOrEdit
          :id="curId"
          ref="deptRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
        />

      </el-dialog>
    </div>
  </div>

</template>

<script>
import addOrEdit from './deptDialog.vue'
import { getTreeData } from '@/utils'
import { getDepartments, delDepartments } from '@/api/departments'
export default {
  components: { addOrEdit },
  data() {
    return {
      list: [], // tree中的数据
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []// 保存传个子组件的部门列表
    }
  },
  async  created() {
    this.initDepartments()
  },
  methods: {
    hClose() {
      this.showDialog = false
      this.$refs.deptRef.resetForm()
    },
    async hDel(id) {
      try {
        const res = await delDepartments(id)
        // console.log(res)
        if (res.code === 10000) {
          this.initDepartments()
          this.$message.success(res.message)
        }
      } catch (error) {
        console.log(error)
      }
    },

    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
      // console.log(id)
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      // 解决数据不更新
      this.$nextTick(() => {
        this.$refs.deptRef.LoadingDepartmentsDetail()
      })
    },
    async initDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.originList = res.data.depts.map(item => {
          const { id, code, name, pid } = item
          return { id, code, name, pid }
        })
        res.data.depts.shift()
        this.list = getTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    hSuccess() {
      // 重新渲染列表数据，关闭弹层
      this.showDialog = false
      this.initDepartments()
    }
  }

}
</script>
